package cn.com.controller;

import cn.com.beans.ActivationCode;
import cn.com.beans.PAccountRole;
import cn.com.beans.VipActivateReturn;
import cn.com.service.ActivationCodeService;
import cn.com.service.CollegeInfoService;
import cn.com.service.PAccountRoleService;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/vip")
public class VIPController {
    @Resource
    private ActivationCodeService activationCodeService;

    @Resource
    private PAccountRoleService pAccountRoleService;

    @GetMapping("/VipActivate")
    public String homeActivate(){
        return "vip/VipActivate";
    }

    @GetMapping("/OpenVip")
    public String homeOpen(){
        return "vip/OpenVip";
    }

    @GetMapping("/VipPayment")
    public String homePay(){
        return "vip/VipPayment";
    }

    @GetMapping("/activate/{codeValue}")
    @ResponseBody
    public List<VipActivateReturn> getactivate(@PathVariable String codeValue){
        ActivationCode activationCode = new ActivationCode();
        activationCode.setAcCode(codeValue);
        activationCode.setAcType(0);
        List<ActivationCode> activationCodes = activationCodeService.selectByACtivationCode(activationCode);
        VipActivateReturn vipActivateReturn = new VipActivateReturn();
        if(activationCodes.size() == 0){
            vipActivateReturn.setMsg("激活码错误");
            vipActivateReturn.setType(0);
        }else{
            vipActivateReturn.setMsg("激活码可用");
            vipActivateReturn.setType(1);
//            activationCodes.get(0).setAcType(1);
//            activationCodeService.updateByPrimaryKeySelective(activationCodes.get(0));
        }
        List<VipActivateReturn> list = new ArrayList<>();
        list.add(vipActivateReturn);
        return list;
    }
    @GetMapping("/activation/{codeValue}")
    @ResponseBody
    public List<VipActivateReturn> activation(HttpServletRequest request,@PathVariable String codeValue){
        HttpSession session = request.getSession();
        Integer uaid = (Integer) session.getAttribute("uaid");
        PAccountRole pAccountRole = new PAccountRole();
        pAccountRole.setRoleId(40);
        pAccountRole.setAccountId(uaid);
        System.out.println("uaid = " + uaid);
        int i1 = pAccountRoleService.updateByAccountId(pAccountRole);
        List<VipActivateReturn> list = new ArrayList<>();
        VipActivateReturn vipActivateReturn = new VipActivateReturn();
        if(i1 == 0){
            System.out.println("修改角色表失败");
            vipActivateReturn.setMsg("激活失败");
            vipActivateReturn.setType(0);
            list.add(vipActivateReturn);
            return list;
        }else{
            vipActivateReturn.setMsg("激活成功");
            vipActivateReturn.setType(1);
//            activationCodes.get(0).setAcType(1);
//            activationCodeService.updateByPrimaryKeySelective(activationCodes.get(0));
        }

        ActivationCode activationCode = new ActivationCode();
        activationCode.setAcCode(codeValue);
        System.out.println();
        activationCode.setAcType(0);
        List<ActivationCode> activationCodes = activationCodeService.selectByACtivationCode(activationCode);
        Integer acId = activationCodes.get(0).getAcId();
        activationCode.setAcType(1);
        activationCode.setAcId(acId);
        int i = activationCodeService.updateByPrimaryKeySelective(activationCode);
        if(i == 0){
            System.out.println("修改激活码失败");
            vipActivateReturn.setMsg("激活失败");
            vipActivateReturn.setType(0);
        }else{
            vipActivateReturn.setMsg("激活成功");
            vipActivateReturn.setType(1);
//            activationCodes.get(0).setAcType(1);
//            activationCodeService.updateByPrimaryKeySelective(activationCodes.get(0));
        }
        list.add(vipActivateReturn);
        return list;
    }

    @GetMapping("/pay")
    @ResponseBody
    public List<VipActivateReturn> activation(HttpServletRequest request){
        System.out.println("进入ajax");
        HttpSession session = request.getSession();
        Integer uaid = (Integer) session.getAttribute("uaid");
        PAccountRole pAccountRole = new PAccountRole();
        pAccountRole.setRoleId(40);
        pAccountRole.setAccountId(uaid);
        int i1 = pAccountRoleService.updateByAccountId(pAccountRole);
        List<VipActivateReturn> list = new ArrayList<>();
        VipActivateReturn vipActivateReturn = new VipActivateReturn();
        if(i1 == 0){
            vipActivateReturn.setMsg("激活失败");
            vipActivateReturn.setType(0);
            list.add(vipActivateReturn);
            return list;
        }else{
            vipActivateReturn.setMsg("激活成功");
            vipActivateReturn.setType(1);
//            activationCodes.get(0).setAcType(1);
//            activationCodeService.updateByPrimaryKeySelective(activationCodes.get(0));
        }
        list.add(vipActivateReturn);
        return list;
    }
}
